<?php

	CLASS AKB_TREE_PANEL extends AKB_PANEL
	{
		var $_htmlFile;

		var $_cats = array();

		var $_reassign = array();

		var $_counter = 0;

		var $_current = 0;

		var $_indent = '';

		/**
		* SetPanelSettings
		* This function is a chance to set any last minute global variables for
		* the template to use
		*
		* @return void
		*/
		function SetPanelSettings()
		{

			$GLOBALS['SearchCategoryOptions'] = $GLOBALS['AKB_CLASS_HELPER']->GetCategoryOptions($this->_cats);
			// Parse the panel of tokens, etc
			$GLOBALS['Favorites'] = $this->_GetFavoritesTree();
			$GLOBALS['FormAction'] = GetUrl('search');

			// Do we need to hide the contact panel?
			if (!$GLOBALS['enableContactPage']) {
				$GLOBALS['HideContactTab'] = 'none';
			} else {
				$GLOBALS['HideContactTab'] = 'block';
			}

			if (isset($_GET['Favs'])) {
				$GLOBALS['FavPanel'] = 'on';
			}

			$GLOBALS['ratedOptions'] = $GLOBALS['AKB_CLASS_HELPER']->getRatedBracketOptions('');

		}


		/**
		* _GetFavoritesTree
		* Returns the code to generate the tree with the favorite questions in it
		*
		* @return string The html for the tree
		*/
		function _GetFavoritesTree()
		{
			$id = 1000;

			// Let's actually build the JavaScript string
			$treeString = $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('FavTreeHeader');

			if (isset($_COOKIE[$GLOBALS['cookiePrefix'].'favs'])) {
				$favs = $_COOKIE[$GLOBALS['cookiePrefix'].'favs'];
				$arrFavs = explode(',', $favs);
				
				$workflowQry = '';
				if (validUrl()) {
					$workflowQry = 'AND (q.workflowstatus = \''.WF_STATUS_APPROVED.'\' OR q.workflowstatus = \''.WF_STATUS_AUTO_APPROVED.'\' OR q.workflowstatus = \''.WF_STATUS_REPENDING.'\' '.getShowDisapprovedArticleString('OR', 'q.').') ';
				}
				foreach ($arrFavs as $f) {

					$query = sprintf("select * from %squestions q
					WHERE q.visible = '1' 
					".$workflowQry."
					and q.questionid='%d'", $GLOBALS['tablePrefix'], $f);
					$result = $GLOBALS['AKB_DB']->Query($query);

					if ($row = $GLOBALS['AKB_DB']->Fetch($result)) {
						$tmpRow = array();
						$historyQuestionIds = array();
						$validStatus = getShowDisapprovedArticleArray();
						if (validUrl()) {
							if (!in_array($row['workflowstatus'], $validStatus)) {
								$tmpRow[] = $row;
							} else {
								$row['title'] = '';
								$tmpRow[] = $row;
								$historyQuestionIds[] = trim($row['questionid']);
							}
						} else {
							$tmpRow[] = $row;
						}
						if (sizeof($historyQuestionIds)) {
							$f = (int)$f;
							if (!is_numeric($f)) {
								$f = 0;
							}
							$historyQuestionIdsStr = "'" . implode("','", $historyQuestionIds) . "'";
							
							$historyQuestionIdsQry = "SELECT  qh.questionid, qh.title, qh.lastupdated, qh.workflowstatus 
							FROM {$GLOBALS['tablePrefix']}questions_history qh, {$GLOBALS['tablePrefix']}catassoc_history c, {$GLOBALS['tablePrefix']}categoryassociations ca
							WHERE (qh.workflowstatus = '".WF_STATUS_AUTO_APPROVED."' OR qh.workflowstatus = '".WF_STATUS_APPROVED."')
							AND qh.questionid = c.questionid 
							AND ca.questionid = c.questionid 
							AND ca.categoryid = c.categoryid 
							AND qh.questionid IN ($historyQuestionIdsStr)
							ORDER BY qh.lastupdated ASC
							";
							
							$historyQuestionIdsQryRes = $GLOBALS['AKB_DB']->Query($historyQuestionIdsQry);
							$historyQuestion = array();
							if ($GLOBALS['AKB_DB']->CountResult($historyQuestionIdsQry) > 0) {
								while ($row = $GLOBALS['AKB_DB']->Fetch($historyQuestionIdsQryRes)) {
									$historyQuestion[$row['questionid']] = $row;
								}
							}
						}
						
						foreach ($tmpRow as $row) {
							if (isset($historyQuestion[$row['questionid']])) {
								$row['title'] = $historyQuestion[$row['questionid']]['title'];
								$row['lastupdated'] = $historyQuestion[$row['questionid']]['lastupdated'];
								$row['workflowstatus'] = $historyQuestion[$row['questionid']]['workflowstatus'];
							}
							if ($row['title'] != '') {
								$cleanTitle = str_replace(array("\r", "\n"), ' ', $row['title']);
								$cleanTitle = htmlspecialchars($cleanTitle, ENT_QUOTES, $GLOBALS['charset']);
								$cleanTitle = $GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders($cleanTitle);
		
								$GLOBALS['TreeId'] = $id;
								$GLOBALS['Title'] = $cleanTitle;
								$GLOBALS['Link'] = GetUrl('question', $f);
								$treeString .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('FavTreeItem');
								$id++;
							}
						}
					}
				}	
			} else {
				$GLOBALS['TreeId'] = $id;
				$GLOBALS['Title'] = GetLang('questNoFavs');
				$GLOBALS['Link'] = 'favorites.php';
				$treeString .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('FavTreeItem');
				$id++;
			}

			$treeString .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('FavTreeFooter');

			return $treeString;
		}

	}

?>
